﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Web;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace DemoExtNet.Base
{
    public partial class SimpleDetailBase : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void InitializeControls()
        {
            this.btnClose.Listeners.Click.Handler = this.wdSimpleDetail.ClientID + ".hide();";
        }

        public void ShowDialog(int id,Viewer.Base.Config.ViewerConstant.DetailsFormType type)
        {
            this.__Id.Text = id.ToString();
            switch (type)
            {
                case Viewer.Base.Config.ViewerConstant.DetailsFormType.None:
                    break;
                case Viewer.Base.Config.ViewerConstant.DetailsFormType.Add:
                    this.SetUpUIForAddNewDialog();
                    break;
                case Viewer.Base.Config.ViewerConstant.DetailsFormType.Edit:
                    this.SetUpUIForEditDialog();
                    break;
                case Viewer.Base.Config.ViewerConstant.DetailsFormType.View:
                    this.SetUpUIForViewDialog();
                    break;
                case Viewer.Base.Config.ViewerConstant.DetailsFormType.Copy:
                    OnClick_Copy(null, null);
                    return;

                default:
                    break;
            }
            this.LoadData();
            this.BindData();
            wdSimpleDetail.Show();
        }

        public void HideDialog()
        {
            wdSimpleDetail.Hide();
        }

        public override void BindData()
        {
            NSToChuc entity = NSToChuc.GetById(int.Parse(__Id.Text));
            if (entity == null) entity = new NSToChuc();
            this.txtFullName.SetValue(entity.TenToChucFull);
            this.txtShortName.SetValue(entity.TenToChucShort);
            this.cbbXepHang.SetValue(entity.XepHangDonViID);
            this.cbbKhoi.SetValue(entity.KhoiQuanLyID);
            this.cboLoaiToChuc.SetValue(entity.LoaiToChucID);
            this.txtGhiChu.SetValue(entity.GhiChu);
            FormPanel1.ClearInvalid();
        }

        public override void LoadData()
        {
           // Viewer.BusinessObject.BusinessEntities.CommonCode common = new Viewer.BusinessObject.BusinessEntities.CommonCode();            
            //this.cbbXepHang.SetDataSource(common.LoadDataCommonByCodeKey(common.XepHangDonVi));
            //this.cbbKhoi.SetDataSource(common.LoadDataCommonByCodeKey(common.KhoiQuanlyToChuc));
            //this.cboLoaiToChuc.SetDataSource (common.LoadDataCommonByCodeKey(common.LoaiToChuc));
            this.cbbXepHang.DataBind();
            this.cbbKhoi.DataBind();
            this.cboLoaiToChuc.DataBind();
        }

        public override void SaveData()
        {
            NSToChuc entity = NSToChuc.GetById(int.Parse(__Id.Text));
            if (entity == null) entity = new NSToChuc();
            entity.TenToChucFull = this.txtFullName.Text.ToString();
            entity.TenToChucShort = this.txtShortName.Text.ToString();
            entity.XepHangDonViID = int.Parse(this.cbbXepHang.Value.ToString());
            entity.KhoiQuanLyID = int.Parse(this.cbbKhoi.Value.ToString());
            entity.LoaiToChucID = int.Parse(this.cboLoaiToChuc.SelectedItem.Value.ToString());
            entity.GhiChu = this.txtGhiChu.Text.ToString();
            entity.DiaChiID = 1;
            entity.Save();
        }

        public override void SetUpUIForAddNewDialog()
        {
            base.SetUpUIForAddNewDialog();
            this.wdSimpleDetail.Title = "Add New";
            this.txtFullName.ReadOnly = false;
            this.txtShortName.ReadOnly = false;
            this.cbbXepHang.ReadOnly = false;
            this.cbbKhoi.ReadOnly = false;
            this.cboLoaiToChuc.ReadOnly = false;
            this.txtGhiChu.ReadOnly = false;
            this.btnSave.Show();
        }

        public override void SetUpUIForEditDialog()
        {
            base.SetUpUIForEditDialog();
            this.wdSimpleDetail.Title = "Edit";
            this.txtFullName.ReadOnly = false;
            this.txtShortName.ReadOnly = false;
            this.cbbXepHang.ReadOnly = false;
            this.cbbKhoi.ReadOnly = false;
            this.cboLoaiToChuc.ReadOnly = false;
            this.txtGhiChu.ReadOnly = false;
            this.btnSave.Show();
        }

        public override void SetUpUIForViewDialog()
        {
            base.SetUpUIForViewDialog();
            this.wdSimpleDetail.Title = "Detail";
            this.txtFullName.ReadOnly = true;
            this.txtShortName.ReadOnly = true;
            this.cbbXepHang.ReadOnly = true;
            this.cbbKhoi.ReadOnly=true;
            this.cboLoaiToChuc.ReadOnly=true;
            this.txtGhiChu .ReadOnly=true;
            this.btnSave.Hide();
        }
        

        protected override void OnClick_Save(object sender, DirectEventArgs e)
        {
            this.SaveData();
            this.wdSimpleDetail.Hide();
        }

        protected override void OnClick_Edit(object sender, DirectEventArgs e)
        {

            this.SetUpUIForEditDialog();
        }

        protected override void OnClick_Create(object sender, DirectEventArgs e)
        {
            this.SetUpUIForAddNewDialog();
            this.__Id.Text = "0";
            this.LoadData();
            this.BindData();
            this.wdSimpleDetail.Show();
        }

        [DirectMethod]
        public override void DoDelete()
        {
            NSToChuc entity = new NSToChuc();
            entity = entity.GetByID(int.Parse(this.__Id.Text));
            entity.Delete();
            entity.Save();
            this.wdSimpleDetail.Hide();
        }

        public void DoDelete(int id)
        {
            NSToChuc entity = new NSToChuc();
            entity = entity.GetByID(id);
            entity.Delete();
            entity.Save();
           
        }

        protected override void OnClick_Copy(object sender, DirectEventArgs e)
        {
            this.SetUpUIForAddNewDialog();
            this.LoadData();
            this.BindData();
            this.__Id.Text = "0";
            this.wdSimpleDetail.Show();
        }

       
    }
}

